﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;

namespace WXYW.BLL
{
    public class UpgradeService : RepositoryBase
    {
        public List<UpgradeForm> GetUpgradeFormByStatus(string status)
        {
            List<UpgradeForm> list = GetEntities<UpgradeForm>(p => p.Status == status).ToList();
            foreach (UpgradeForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
            }

            return list;
        }

        public int GetNumberOfUpgrade(string upCode)
        {
            return GetEntities<UpgradeForm>(u => u.Code.Contains(upCode)).Count();
        }

        public void AddUpgradeForm(string prodcutID, string upgradeReason, long applicant, string sendway, string checkResult)
        {    
            ProductService productservice = new ProductService();
            UserService userservice = new UserService();
            User applicantUser = userservice.GetUserByID(applicant);
            if (!applicantUser.ServiceStationReference.IsLoaded)
            {
                applicantUser.ServiceStationReference.Load();
            }
            ApplicationProduct product = productservice.GetProductByID(long.Parse(prodcutID));
            UpgradeForm upForm = new UpgradeForm();
            upForm.ApplicationProduct = product;
            upForm.Code = RunningNumberService.GenerateUpgrateNumber();
            upForm.ServiceStation = applicantUser.ServiceStation;
            upForm.Status = "升级-待审批";
            upForm.ApplyTime = DateTime.Now;
            upForm.UpgradeReason = upgradeReason;
            upForm.SendWay = sendway;
            upForm.User = applicantUser;
            upForm.Remark = checkResult;
            InsertEntity(upForm);

            product.Status = "升级申请中";
            product.User = null;
            productservice.UpdateProduct(product);
        }

        public UpgradeForm GetProductDetailForm(long upID)
        {
            UpgradeForm upForm = GetUpgradeFormByID(upID);
            if (!upForm.ApplicationProductReference.IsLoaded)
            {
                upForm.ApplicationProductReference.Load();
            }
            if (!upForm.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!upForm.UserReference.IsLoaded)
            {
                upForm.UserReference.Load();
            }
            if (!upForm.ApplicationProduct.ProblemTypeReference.IsLoaded)
            {
                upForm.ApplicationProduct.ProblemTypeReference.Load();
            }
            if (!upForm.ApplicationProduct.ProductTypeReference.IsLoaded)
            {
                upForm.ApplicationProduct.ProductTypeReference.Load();
            }

            return upForm;
        }

        public UpgradeForm GetUpgradeFormByID(long ID)
        {
            return GetEntities<UpgradeForm>(u => u.ID == ID).FirstOrDefault();
        }

        public void Update(UpgradeForm upForm)
        {
            UpdateEntity(upForm);
        }

        public List<UpgradeForm> GetFormByServiceStationID(int stationID, string keywords)
        {
            List<UpgradeForm> list = new List<UpgradeForm>();
            if (!string.IsNullOrEmpty(keywords))
            {
                list = GetEntities<UpgradeForm>(u => u.ServiceStation.ID == stationID).Where(u => u.ApplicationProduct.ProductSerialNumber.Contains(keywords) ||
                u.Code.Contains(keywords) || u.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keywords) ||
                u.ApplicationProduct.ApplicationForm.User.ContactPerson.Contains(keywords) ||
                u.ApplicationProduct.ApplicationForm.User.CompanyName.Contains(keywords) ||
                u.ServiceStation.Name.Contains(keywords) ||
                u.Status.Contains(keywords) ||
                u.ApplicationProduct.ProductType.ProductTypeName.Contains(keywords)).ToList();
            }
            else
            {
                list = GetEntities<UpgradeForm>().ToList();
            }

            foreach (UpgradeForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
            }

            return list;
        }

        public List<UpgradeForm> GetAll(string keywords)
        {
            List<UpgradeForm> list = new List<UpgradeForm>();
            if (!string.IsNullOrEmpty(keywords))
            {
                list = GetEntities<UpgradeForm>().Where(u => u.ApplicationProduct.ProductSerialNumber.Contains(keywords) ||
                u.Code.Contains(keywords) || u.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keywords) ||
                u.ApplicationProduct.ApplicationForm.User.ContactPerson.Contains(keywords) ||
                u.ApplicationProduct.ApplicationForm.User.CompanyName.Contains(keywords) ||
                u.ServiceStation.Name.Contains(keywords) ||
                u.Status.Contains(keywords) ||
                u.ApplicationProduct.ProductType.ProductTypeName.Contains(keywords)).ToList();
            }
            else
            {
                list = GetEntities<UpgradeForm>().ToList();
            }
            foreach (UpgradeForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
            }

            return list;
        }

        public UpgradeForm GetSendFormDetailByUpgradeID(long upID)
        {
            UpgradeForm upForm = GetUpgradeFormByID(upID);
            if (!upForm.ApplicationProductReference.IsLoaded)
            {
                upForm.ApplicationProductReference.Load();
            }
            if (!upForm.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!upForm.ApplicationProduct.ProductTypeReference.IsLoaded)
            {
                upForm.ApplicationProduct.ProductTypeReference.Load();
            }
            if (!upForm.ApplicationProduct.ProblemTypeReference.IsLoaded)
            {
                upForm.ApplicationProduct.ProblemTypeReference.Load();
            }
            if (!upForm.ApplicationProduct.ApplicationForm.UserReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationForm.UserReference.Load();
            }

            return upForm;
        }

        public List<UpgradeForm> GetUpgrateFoemByKeywords(string keyword)
        {
            List<UpgradeForm> upgrateForms = new List<UpgradeForm>();
            if (!string.IsNullOrEmpty(keyword))
            {
                upgrateForms = GetEntities<UpgradeForm>(u => u.ServiceStation.Name.Contains(keyword)
                    || u.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keyword)
                    || u.ApplicationProduct.ProductType.ProductTypeName.Contains(keyword)
                    || u.ApplicationProduct.ProductSerialNumber.Contains(keyword)
                    || u.ApplicationProduct.RepairAccessory.Contains(keyword)
                    || u.ApplicationProduct.ProblemType.Name.Contains(keyword)
                    ).ToList();
            }
            else
            {
                upgrateForms = GetAll("");
            }

            foreach (UpgradeForm form in upgrateForms)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ProblemTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProblemTypeReference.Load();
                }
            }

            return upgrateForms;
        }

        public List<UpgradeForm> GetUpgradeFormByStatusAndServiceStation(string status, int servicestationID)
        {
            List<UpgradeForm> list = GetEntities<UpgradeForm>(p => p.Status == status && p.ServiceStation.ID == servicestationID).ToList();
            foreach (UpgradeForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
            }

            return list;
        }
    }
}
